package com.base.library.utils;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.TypedValue;

import androidx.annotation.AttrRes;
import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.StyleRes;
import androidx.annotation.StyleableRes;
import androidx.core.content.ContextCompat;

/**
 * 主题操作类
 */
public class ThemeUtils {

    @ColorInt
    public static int resolveColor(Context context, @AttrRes int attr) {
        return resolveColor(context, attr, 0);
    }

    @ColorInt
    public static int resolveColor(Context context, @AttrRes int attr, @ColorInt int defValue) {
        TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr});
        try {
            return a.getColor(0, defValue);
        } finally {
            a.recycle();
        }
    }

    public static ColorStateList resolveColorList(Context context, @AttrRes int attr) {
        TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr});
        try {
            return a.getColorStateList(0);
        } finally {
            a.recycle();
        }
    }

    @ColorInt
    public static int getColor(Context context, @ColorRes int colorId) {
        return ContextCompat.getColor(context, colorId);
    }

    public static int resolveInt(Context context, @AttrRes int attrRes) {
        return resolveInt(context, attrRes, 0);
    }

    public static int resolveInt(Context context, @AttrRes int attrRes, int defaultValue) {
        TypedArray a = context.obtainStyledAttributes(new int[]{attrRes});
        try {
            return a.getInt(0, defaultValue);
        } finally {
            a.recycle();
        }
    }

    public static int resolveResourceId(Context context, @AttrRes int attrRes, int defaultValue) {
        TypedArray a = context.obtainStyledAttributes(new int[]{attrRes});
        try {
            return a.getResourceId(0, defaultValue);
        } finally {
            a.recycle();
        }
    }

    public static float resolveFloat(Context context, @AttrRes int attrRes) {
        TypedValue typedValue = new TypedValue();
        context.getTheme().resolveAttribute(attrRes, typedValue, true);
        return typedValue.getFloat();
    }

    public static float resolveFloat(Context context, @AttrRes int attrRes, float defaultValue) {
        TypedArray a = context.obtainStyledAttributes(new int[]{attrRes});
        try {
            return a.getFloat(0, defaultValue);
        } finally {
            a.recycle();
        }
    }

    public static String resolveString(Context context, @AttrRes int attr) {
        TypedValue v = new TypedValue();
        context.getTheme().resolveAttribute(attr, v, true);
        return (String) v.string;
    }

    public static String resolveString(Context context, @AttrRes int attr, String defaultValue) {
        TypedValue v = new TypedValue();
        context.getTheme().resolveAttribute(attr, v, true);
        String value = (String) v.string;
        return TextUtils.isEmpty(value) ? defaultValue : value;
    }

    public static String resolveString(Resources.Theme theme, @AttrRes int attr) {
        TypedValue v = new TypedValue();
        theme.resolveAttribute(attr, v, true);
        return (String) v.string;
    }


    public static Drawable resolveDrawable(Context context, @AttrRes int attr) {
        return resolveDrawable(context, attr, null);
    }

    public static Drawable resolveDrawable(
            Context context,
            @AttrRes int attr,
            @SuppressWarnings("SameParameterValue") Drawable defValue) {
        TypedArray array = context.getTheme().obtainStyledAttributes(new int[]{attr});
        try {
            Drawable drawable = array.getDrawable(0);
            if (drawable == null && defValue != null) {
                drawable = defValue;
            }
            return drawable;
        } finally {
            array.recycle();
        }
    }

    public static int resolveDimension(Context context, @AttrRes int attr) {
        return resolveDimension(context, attr, -1);
    }

    public static int resolveDimension(Context context, @AttrRes int attr, int def) {
        TypedArray a = context.getTheme().obtainStyledAttributes(new int[]{attr});
        try {
            return a.getDimensionPixelSize(0, def);
        } finally {
            a.recycle();
        }
    }

    public static int resolveDimension(Context context, @StyleableRes int[] attrs, @StyleRes int resId, @StyleableRes int index) {
        final TypedArray appearance = context.obtainStyledAttributes(
                resId, attrs);
        int value = appearance.getDimensionPixelSize(index, 0);
        appearance.recycle();
        return value;
    }

    public static boolean resolveBoolean(Context context, @AttrRes int attrRes) {
        return resolveBoolean(context, attrRes, false);
    }

    public static boolean resolveBoolean(Context context, @AttrRes int attrRes, boolean defaultValue) {
        TypedArray a = context.obtainStyledAttributes(new int[]{attrRes});
        try {
            return a.getBoolean(0, defaultValue);
        } finally {
            a.recycle();
        }
    }
}
